package com.lanou.service.impl;

import com.lanou.entity.*;
import com.lanou.mapper.*;
import com.lanou.service.ItemOperationService;
import com.lanou.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;


@Service
public class ItemOperationServiceImpl implements ItemOperationService {
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    @Value("${ITEM_JSON_URL}")
    private String ITEM_JSON_URL;
    @Autowired
    private JedisPool jedisPool;
    @Value("${ITEM_UPDATE_URL}")
    private String ITEM_UPDATE_URL;
    @Value("${CART_KEY_PREFIX}")
    private String CART_KEY_PREFIX;

    @Override
    public boolean deleteById(long[] ids) {
        for (Long id : ids) {
            int i = tbItemMapper.deleteByPrimaryKey(id);
            if (i == 0) {
                return false;
            }
            //通过id删除redis中的item
            HttpClientUtils.getRaw(ITEM_JSON_URL + "/cache?" + id);
        }
        return true;
    }

    @Override//修改商品
    public boolean instock(long[] ids) {
        for (long id : ids) {
            TbItem tbItem = new TbItem();
            tbItem.setStatus((byte) 2);
            TbItemExample tbItemExample = new TbItemExample();
            tbItemExample.createCriteria().andIdEqualTo(id);
            int i = tbItemMapper.updateByExampleSelective(tbItem, tbItemExample);
            if (i == 0) {
                return false;
            }
            HttpClientUtils.getRaw(ITEM_UPDATE_URL+"/cache?"+id);
        }
        return true;
    }

    @Override
    public boolean reshelf(long[] ids) {

        for (long id : ids) {
            TbItem tbItem = new TbItem();
            tbItem.setStatus((byte) 1);
            TbItemExample tbItemExample = new TbItemExample();
            tbItemExample.createCriteria().andIdEqualTo(id);
            int i = tbItemMapper.updateByExampleSelective(tbItem, tbItemExample);
            if (i == 0) {
                return false;
            }
            HttpClientUtils.getRaw(ITEM_UPDATE_URL+"?"+id);
        }
        return true;
    }

    //编辑商品
    public boolean updateByIds(TbItem tbItem, String desc, String itemParams, Long itemParamId) {
        Date date = new Date();
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setUpdated(date);
        tbItem.setUpdated(date);
        TbItemExample tbItemExample = new TbItemExample();
        tbItemExample.createCriteria().andIdEqualTo(tbItem.getId());
        tbItemMapper.updateByExampleSelective(tbItem, tbItemExample);
        TbItemDescExample tbItemDescExample = new TbItemDescExample();
        tbItemDescExample.createCriteria().andItemIdEqualTo(tbItemDesc.getItemId());
        tbItemDescMapper.updateByExampleSelective(tbItemDesc, tbItemDescExample);
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);
        TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
        tbItemParamItemExample.createCriteria().andIdEqualTo(itemParamId);
        tbItemParamItemMapper.updateByExampleSelective(tbItemParamItem, tbItemParamItemExample);
        HttpClientUtils.getObject(ITEM_UPDATE_URL+"/cache?"+tbItem.getId(),Map.class);
        return true;
    }
}
